﻿using UnityEngine;
using System.Collections;

public class LogInManager : MonoBehaviour 
{
	private GUIState state;
		
	public Texture texture;
	
	private FacebookController facebookController;

	private enum GUIState
	{
		Idle,
		Login,
		Loading,
		FailedPopup
	}

	private string userName = string.Empty;

	private void Awake()
	{		
		facebookController = new FacebookController();
	}

	private void OnGUI()
	{
		GUI.DrawTexture(new Rect(0f, 0f, Screen.width, Screen.height), texture);

		switch (state)
		{
			case GUIState.Idle:
			{
				IdleState();
				break;
			}
				
			case GUIState.Login:
			{
				LoginState();
				break;
			}
				
			case GUIState.Loading:
			{
				LoadingState();
				break;
			}
				
			case GUIState.FailedPopup:
			{
				FailedPopupState();
				break;
			}

			default:
			{
				IdleState();
				break;
			}
		}
	}

	private void IdleState()
	{
		if (GUILayout.Button("Facebook Log In"))
		{
			SoundSystem.Instance.PlaySoundByName("ButtonTap");

			FacebookLogIn ();
		}
		
		if (GUILayout.Button("Normal Log In"))
		{
			SoundSystem.Instance.PlaySoundByName("ButtonTap");

			state = GUIState.Login;
		}
	}

	private void LoginState()
	{
		userName = GUILayout.TextArea(userName, Constants.MAX_LENGHT_NAME);

		if (GUILayout.Button("Accept") && !string.IsNullOrEmpty(userName))
		{
			PlayerPrefs.SetInt("FirstTime", Constants.USER_CREATED);
			PlayerPrefs.SetInt("Sound", Constants.SOUND_ENABLED);
			
			PlayerPrefs.SetString("UserName", userName);

			Application.LoadLevel(Constants.SCENE_MAIN);
		}

		if (GUILayout.Button("Back"))
		{
			state = GUIState.Idle;
		}
	}
	
	private void LoadingState()
	{
		GUILayout.TextField("Cargando. Por favor Espere...");
	}

	private string failedPopupText;
	private void FailedPopupState()
	{		
		GUILayout.TextField("Fallo");
		GUILayout.TextField(failedPopupText);

		if (GUILayout.Button("Back"))
		{
			state = GUIState.Idle;
		}
	}

#region LogInCode
	private bool LoggingIn;
	public void FacebookLogIn()
	{
		if (LoggingIn)
			return;
		#if UNITY_IPHONE || UNITY_ANDROID
		if (Application.internetReachability != NetworkReachability.NotReachable)
		{		
			if (facebookController.IsLogged())
			{
				facebookController._Me = null;
				facebookController.ClearFriends ();
				facebookController.LogOut ();
			}
			if (!facebookController.IsLogged())
			{
				LoggingIn = true;
				Debug.Log("LOG INNN");

				state = GUIState.Loading;
				
				facebookController.LogIn ();
				
				FacebookManager.CleanLogInActions ();
				FacebookManager.sessionOpenedEvent += SuccesLogInCallback;
				FacebookManager.loginFailedEvent += FailedLogInCallback;
			}
			else
			{				
				Debug.Log("CANT LOG INNN" + (!facebookController.IsLogged()));
			}
		}
		else
		{
			failedPopupText = "No Internet error";
			state = GUIState.FailedPopup;
		}
		#endif
	}

	public void SuccesLogInCallback()
	{
		Debug.Log("SuccessLogInCallback");	
		#if UNITY_IPHONE || UNITY_ANDROID
		LoggingIn = false;
		facebookController.GetMe ();
		
		facebookController.CleanGraphActions ();
		facebookController.graphRequestCompletedEvent += GetMeCallbackSuccess;
		facebookController.graphRequestFailedEvent += GraphRequestedFailed;
		#endif
	}

	public void FailedLogInCallback(Prime31.P31Error err)
	{		
		Debug.Log("FailedLogInCallback");	
		#if UNITY_IPHONE || UNITY_ANDROID
		LoggingIn = false;
		failedPopupText = "LogIn error";
		state = GUIState.FailedPopup;
		#endif
	}
#endregion


#region GetUserInfo
	public void GetMeCallbackSuccess()
	{
		Debug.Log("GetMeCallbackSuccess");	
		
		#if UNITY_IPHONE || UNITY_ANDROID
		facebookController.graphRequestCompletedEvent -= GetMeCallbackSuccess;
		#endif
		
		FacebookUserConnected();		
	}	

	private void FacebookUserConnected()
	{				
		#if UNITY_IPHONE || UNITY_ANDROID
		
		PlayerPrefs.SetInt("FirstTime", Constants.USER_CREATED);
		PlayerPrefs.SetInt("Sound", Constants.SOUND_ENABLED);
		
		PlayerPrefs.SetString("UserName", facebookController._Me.name);		
		PlayerPrefs.SetString("FacebookID", facebookController._Me.id);

		Application.LoadLevel(Constants.SCENE_MAIN);
		#endif
	}

	public void GraphRequestedFailed()
	{
		Debug.Log("GraphRequestedFailed");	
		failedPopupText = "GraphRequestedFailed";
		state = GUIState.FailedPopup;
	}
#endregion
}
